<?php
    /* Read the table */

    $infile = "rtdpht.txt";
    $handle = fopen($infile, 'r');
    $contents = fread($handle, filesize($infile));     
    fclose($handle);

    /* Open files for def and lexer class */
    $def = fopen("gztrans.def", 'w');
    if ($def === FALSE)
    {
        exit(1);
    }
    $plex = fopen("gztrans.plex", 'w');
    if ($def === FALSE)
    {
        exit(1);
    }

    /* Convert the file into an array */

    $lines = explode("\n", $contents);
    $count = count ($lines);
    for($i = 0; $i < $count;$i++)
    {
        if (preg_match("/^#.*/", $lines[$i]) || preg_match("/^[\s\n]*$/", $lines[$i]))
         {
             unset($lines[$i]);
         }
    }

    /* Generate the header */
    fwrite($def, "<?php\n");
    fwrite($plex, "<?php\n");

    /* Add the constants */
    fwrite($def, "define(\"_WHITESPACE\", 0);\n");
    fwrite($def, "define(\"_COMMA\",      1);\n");
    fwrite($def, "define(\"_HYPHEN\",     2);\n");
    fwrite($def, "define(\"_NEWLINE\",    3);\n");
    $i = 4;
    foreach ($lines as $line)
    {
        $tokens = split("[\t ]+", $line);
        fwrite($def, "define(\"_phoneme$tokens[0]\", $i);\n");
        $i++;
    }

    fwrite($def, "\$trtable = array (\n");
    $i = 4;
    foreach ($lines as $line)
    {
        $tokens = split("[\t ]+", $line);
        fwrite($def, "\"_phoneme$tokens[0]\"=>, array($tokens[2], $tokens[3]),\n");
        $i++;
    }
    fwrite($def, ")\n");

    /* Print the Class file */
    fwrite($plex, "class HTrans {\n");
    fwrite($plex, "    public  \$output;\n");
    fwrite($plex, "    private \$data;\n");
    fwrite($plex, "    private \$N;\n");
    fwrite($plex, "    private \$token;\n");
    fwrite($plex, "    private \$value;\n");
    fwrite($plex, "    private \$line;\n");
    fwrite($plex, "    private \$state = 1;\n");
    fwrite($plex, "    function __construct(\$data)\n");
    fwrite($plex, "{\n");
    fwrite($plex, "    \$this->data = \$data;\n");
    fwrite($plex, "    \$this->N     = 0;\n");
    fwrite($plex, "    \$this->line  = 1;\n");
    fwrite($plex, "}\n");


    /* Generate the token identifiers */
    fwrite($plex, "/*!lex2php\n");
    fwrite($plex, "%input   \$this->data\n");
    fwrite($plex, "%counter \$this->N\n");
    fwrite($plex, "%token   \$this->token\n");
    fwrite($plex, "%value   \$this->value\n");
    fwrite($plex, "%line    \$this->line\n");
    fwrite($plex, "whitespace = /[ \\t]+/\n");
    fwrite($plex, "newline    = /\\n/\n");
    fwrite($plex, "hyphen     = \"-\"\n");
    fwrite($plex, "comma      = \",\"\n");
    foreach ($lines as $line)
    {
        $tokens = split("[\t ]+", $line);
        fwrite($plex, "gztoken$tokens[0]    = \"$tokens[1]\"\n");
    }
    fwrite($plex, "*/\n");

    /* Generate the return statements */
    fwrite($plex, "/*!lex2php\n");
    fwrite($plex, "whitespace { \$this->output = _WHITESPACE;}\n");
    fwrite($plex, "newline    { \$this->output = _NEWLINE;}\n");
    fwrite($plex, "hyphen     { \$this->output = _HYPHEN;}\n");
    fwrite($plex, "comma      { \$this->output = _COMMA;}\n");
    foreach ($lines as $line)
    {
        $tokens = split("[\t ]+", $line);
        fwrite($plex, "gztoken$tokens[0]    { \$this->output = _phoneme$tokens[0]; }\n");
    }


    /* Generate the footer */
    fwrite($plex, "*/\n");
    fwrite($plex, "}\n");
    fwrite($def, "?>\n");
    fwrite($plex, "?>\n");

    fclose($def);
    fclose($plex);
?>

